/*
 * Ext GWT - Ext for GWT
 * Copyright(c) 2007-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
package com.hy.mydesktop.client.mvc.desktop;


import java.util.HashMap;
import java.util.Map;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.desktop.client.Shortcut;
import com.extjs.gxt.desktop.client.StartMenu;
import com.extjs.gxt.desktop.client.TaskBar;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hy.mydesktop.client.MyDesktop;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.factory.GxtDesktopComponentFactory;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.client.mvc.core.event.manager.ControllerForwardUtil;
import com.hy.mydesktop.client.mvc.util.ControllerUtil;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;


/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-9-21；时间：下午02:12:20</li>
 * <li>类型名称：DesktopController</li>
 * <li>设计目的：DesktopController 控制器</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class DesktopController extends BaseController {

	Desktop desktop = null;
	private DesktopPresenter desktopPresenter;

  public DesktopController() {
    registerEventTypes(AppEvents.DESKTOP_WINDOWAPPLICATION_INIT);
		/**
		 * 是否应该在此位置,先创建控件
		 * 是否大多数情况下,都都适合在这里创建控件
		 * 对于个别的,不想要在这里创建控件的情况,如何处理
		 */
	  MyLoggerUtil.printControllerDebugForInit(getClass());
		

//根据事件中包含的数据，响应事件，即创建桌面快捷方式
			
			/**
			 * data 可以是需要加载的菜单的元模型信息
			 *     也可以是用户信息，调用后台业务逻辑，获得需要加载的菜单的元模型信息
			 */
			
			
			/**
			 * 何时添加控件的事件
			 * 1：创建控件的过程中，添加
			 * 识别GxtComponentMetaNodeModel中，Map中某个指，来设置它是否有AppEvent事件，
			 * 如何有AppEvent事件，需要被监听。
			 * 需要在Shortcut工厂中，通过ShortcutModelEnum.AppEven来判断，是否有事件需要监听
			 * 如果判断有，
			 * 根据Shortcut特定的方式，添加事件监听。如 Shortcut.addListener()
			 * 
			 * 2：控件创建完成后，添加
			 * 控件创建完成后，返回的是一个Desktop。
			 * 如何知道，改为哪个控件添加事件
			 */
			/**
			 * 为每个快捷方式，注册单击事件（包括业务事件）
			 */
	  /*if (isInitSelfComponent()) {
			initialize();
			super.initialized = true;
	}*/
  }
  
  public void handleEvent(AppEvent event) {
    EventType type = event.getType();
//	EventStructureMetaModel eventStructureMetaModel =(EventStructureMetaModel)((Map<String, Object>)event.getData()).get("eventstructuremetamodel");
	EventStructureMetaModel eventStructureMetaModel =event.getData(	ComponentMetaDataConstants.EVENT_STRUCTURE_METAMODEL);

	if (ControllerUtil.isRootHandle(eventStructureMetaModel, this)) {
		//0002
	     if (type == AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER) {
	    	 if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
	    	//event.getData();??
	    	 //onInitDesktop(event);
	    		forwardToView(desktopPresenter, event);
	    	 if (desktop==null) {
				desktop = new Desktop();
			}
	    	 Object object = event.getData();
	    	 Map<String, Object> map = new HashMap<String, Object>();
//	    	 map.put("gxtComponentMetaModel", object);
	    	 map.put("desktop", desktop);
	    	 map.put("eventstructuremetamodel", new EventStructureMetaModel());    	 
	    	 //对AppEvent进行二次封装
	    	 this.forwardToChild(new AppEvent( AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER, map));
	    	 }
	    } 
	     //0003
	    /* else if (type == AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT) {
	    	 if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
					forwardToView(desktopView, event);
					this.forwardToChild(event);
				}
	     } */
	     //0005
	     else  if (type == AppEvents.DESKTOP_MENUITEM_ONCLICK_INIT) {
		    	EventStructureMetaModel eventStructureMetaModel2 =event.getData(	ComponentMetaDataConstants.EVENT_STRUCTURE_METAMODEL);

				if (ControllerUtil.isHandle(eventStructureMetaModel2, this)) {
					 /*MyLoggerUtil.printDebug(this.getClass(), "该控制器【id=" +this.getId()+
					 		"】， 转发 AppEvents.DESKTOP_MENUITEM_ONCLICK_INIT 事件，到" +
					 		"desktopPresenter"+
					 		"");*/
					ControllerForwardUtil.printControllerDebugForEventForward(this, event);
					forwardToView(desktopPresenter, event);
//					this.forwardToChild(event);
				}
	    	
	     } 
	   //0007
	     else  if (type == AppEvents.DESKTOP_WINDOWAPPLICATION_INIT) {
		    	EventStructureMetaModel eventStructureMetaModel2 =event.getData(ComponentMetaDataConstants.EVENT_STRUCTURE_METAMODEL);

				if (ControllerUtil.isHandle(eventStructureMetaModel2, this)) {
					 /*MyLoggerUtil.printDebug(this.getClass(), "该控制器【id=" +this.getId()+
					 		"】， 转发 AppEvents.DESKTOP_MENUITEM_ONCLICK_INIT 事件，到" +
					 		"desktopPresenter"+
					 		"");*/
					ControllerForwardUtil.printControllerDebugForEventForward(this, event);
					forwardToView(desktopPresenter, event);
//					this.forwardToChild(event);
				}
	    	
	     } 
	    //转发给子节点
	     else {
				this.forwardToChild(event);
		 }
	     
	     
	}
   
     
  }
  
  public void initialize() {
	  if (desktop==null) {
		  desktop = new Desktop();		
		  TaskBar taskBar = desktop.getTaskBar();
			 StartMenu startMenu = taskBar.getStartMenu();
			 startMenu.setIconStyle("user");
			 startMenu.setHeading("开始菜单");//The StartMenu
			// tools
			    MenuItem tool = new MenuItem("Settings");
			    tool.setIcon(IconHelper.createStyle("settings"));
			    tool.addSelectionListener(new SelectionListener<MenuEvent>() {
			      @Override
			      public void componentSelected(MenuEvent ce) {
			        Info.display("Event", "The 'Settings' tool was clicked");
			      }
			    });
			    startMenu.addTool(tool);

			    startMenu.addToolSeperator();

			    tool = new MenuItem("注销");//Logout
			    tool.setIcon(IconHelper.createStyle("logout"));
			    tool.addSelectionListener(new SelectionListener<MenuEvent>() {
			      @Override
			      public void componentSelected(MenuEvent ce) {
			        Info.display("Event", "The 'Logout' tool was clicked");
			      }
			    });
			    startMenu.addTool(tool);
	}
	MyLoggerUtil.printComponentDebugForInit(Desktop.class);

	  Registry.register(MyDesktop.DESKTOP, desktop);

	//GxtComponentMetaModel gxtComponentMetaModel = this.getDesktopMetaModel();
		//GxtDesktopComponentFactory.createGxtComponent(desktop, gxtComponentMetaModel);
	  desktopPresenter = new DesktopPresenter(this, new DesktopView(desktop));
  }

  protected void onError(AppEvent ae) {
    System.out.println("error: " + ae.<Object>getData());
  }
  
  

}
